package edu.pet.Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.User;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		PetDAO dao = new PetDAO();
		User user = new User();
		if("add".equals(type)){
			user.setUsername(request.getParameter("username"));
			user.setPassword(request.getParameter("password"));
			dao.addUser(user);
			request.setAttribute("msg", "保存成功");
			this.getServletContext().getRequestDispatcher("/user/manage/addUser.jsp").forward(request, response);
		}
		if("toEdit".equals(type)){
			HttpSession session = request.getSession();
			user = (User) session.getAttribute("login");
			request.setAttribute("user", user);
			this.getServletContext().getRequestDispatcher("/user/manage/editUser.jsp").forward(request, response);
		}
		if("doEdit".equals(type)){
			user.setUsername(request.getParameter("username"));
			user.setPassword(request.getParameter("password"));
			dao.editUser(user);
			request.setAttribute("msg", "密码修改成功！");
			this.getServletContext().getRequestDispatcher("/user/manage/editUser.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
